<?php

require_once(DIR_LIB."VsGui".DIR_SEP."vssmartywidget.php");

class VsDateEdit extends VsSmartyWidget {

	private static $_objCount = 0;

	private $__objCount = 0;

	private $_day = '00';
	private $_month = '00';
	private $_year = '0000';

	private $_days = 0;



	function __construct(&$page, $date='') {
		// Ne go zabravqj ina4e nqma $smarty Object
		parent::__construct();

		$this->_template = dirname(__FILE__).DIR_SEP."vsdateedit.html";

		$this->_wwwDir = 'templates/XCart/';
		$this->assign('wwwDir', $this->_wwwDir);

		$this->__objCount = ++self::$_objCount;

		if($this->__objCount==1)
			$page->addJsCode($this->jsfuncDateChanged());
		//$page->addJsFile($this->_wwwDir."js/popup_image_selection.js");

		$this->init($date);

		$this->assign('vsdeDay', $this->_day);
		$this->assign('vsdeMonth', $this->_month);
		$this->assign('vsdeYear', $this->_year);

		$this->assign('vsdeDays', $this->_days);
	}
	//============================================================================

	function show() {
		$this->assign('objCount', $this->__objCount);

		return $this->fetch($this->_template);
	}
	//============================================================================

	function date() {
		return $this->_year.'-'.$this->_month.'-'.$this->_day;
	}
	//=============================================================================

	private function init($date) {
		if(isset($_GET['vsde_obj']) && ($_GET['vsde_obj']==$this->__objCount)) {
			$date = explode('-', $_GET['chvsde']);
		}
		else if($date!='') {
			$date = explode('-', $date);
		}
		else {
			$date = explode('-', date('Y-m-d'));
			//print_r($date);
		}

		$this->_year = $date[0];
		$this->_month = $date[1];
		$this->_day = $date[2];

		$this->initDays();
	}
	//===========================================================================

	private function initDays() {
		if(!(basename($_SERVER['PHP_SELF']) == 'operators_work.php')) {
			$this->_days=31;

			return;
		}

		switch(intval($this->_month)) {
			case 2:
				$this->_days=28;
				break;
			case 4:
				$this->_days=30;
				break;
			case 6:
				$this->_days=30;
				break;
			case 9:
				$this->_days=30;
				break;
			case 11:
				$this->_days=30;
				break;
			default:
				$this->_days=31;
		}
	}
	//=============================================================================

	private function jsfuncDateChanged() {
		$qs = explode('&chvsde', $_SERVER['QUERY_STRING']);

		$js = "function dateChanged(objCount) {\n";
		$js .= "	var dayId = 'vsdeDay'+objCount;\n";
		$js .= "	var monthId = 'vsdeMonth'+objCount;\n";
		$js .= "	var yearId = 'vsdeYear'+objCount;\n";
		$js .= "\n";
		$js .= "	var day = document.getElementById(dayId).value;\n";
		$js .= "	if(day.length==1)\n";
		$js .= "		day = '0'+day;\n";
		$js .= "	var month = document.getElementById(monthId).value;\n";
		$js .= "	var year = document.getElementById(yearId).value;\n";
		$js .= "\n";
		$js .= "	//alert(year+'-'+month+'-'+day);\n";
		$js .= "\n";

		//echo $_SERVER['PHP_SELF'];
		if(basename($_SERVER['PHP_SELF']) == 'operators_work.php') {
		//if($_SERVER['SCRIPT_NAME'] == 'operators_work.php') {
			$js .= "	var form = document.vsdeDate;\n";
			$js .= "	if(objCount==1)\n";
			$js .= "		form.fromVsdeDate.value=year+'-'+month+'-'+day;\n";
			$js .= "	else\n";
			$js .= "		form.toVsdeDate.value=year+'-'+month+'-'+day;\n";
			$js .= "\n";

			$js .= "	form.submit();\n";
		}
		else if(basename($_SERVER['PHP_SELF']) == 'models.php') {
			//if($_SERVER['SCRIPT_NAME'] == 'operators_work.php') {
			if(isset($_GET['mode']) && ($_GET['mode']=='work_count')) {
				$js .= "	var form = document.vsdeDate;\n";
				$js .= "	if(objCount==1) {\n";
				$js .= "		form.fromVsdeDate.value=year+'-'+month+'-'+day;\n";
				$js .= "	}\n";
				$js .= "	else {\n";
				$js .= "		form.toVsdeDate.value=year+'-'+month+'-'+day;\n";
				$js .= "	}\n";
				$js .= "\n";

				$js .= "	form.submit();\n";
			}
			else if(isset($_GET['opid'])) {
				$js .= "	var form = document.getElementById('vsdeDate');\n";
				$js .= "	form.value=year+'-'+month+'-'+day;\n";
				$js .= "\n";
			}
			else {
				$js .= "	if(objCount==1) {\n";
				$js .= "		var form = document.getElementById('fromVsdeDate');\n";
				$js .= "		form.value=year+'-'+month+'-'+day;\n";
				$js .= "	}\n";
				$js .= "	else {\n";
				$js .= "		var form = document.getElementById('toVsdeDate');\n";
				$js .= "		form.value=year+'-'+month+'-'+day;\n";
				$js .= "	}\n";
				$js .= "\n";

				//$js .= "	form.submit();\n";
			}
		}
		else if(basename($_SERVER['PHP_SELF']) == 'model_work.php') {
			$js .= "	var form = document.vsdeDate;\n";
			$js .= "	form.date.value=year+'-'+month+'-'+day;\n";
			
			$js .= "	//alert(form.date.value);\n";
			$js .= "	//return;\n";
			
			$js .= "\n";
			$js .= "	form.submit();\n";
		}
		else {
			$js .= "	var fromVsdeDate = document.getElementById('fromVsdeDate');\n";
			$js .= "	var toVsdeDate = document.getElementById('toVsdeDate');\n";
			$js .= "\n";
			$js .= "	if(objCount==1)\n";
			$js .= "		fromVsdeDate.value=year+'-'+month+'-'+day;\n";
			$js .= "	else\n";
			$js .= "		toVsdeDate.value=year+'-'+month+'-'+day;\n";
			$js .= "\n";
			$js .= "\n";
			$js .= "	//alert(toVsdeDate.value);\n";

			//$js .= "	var strLocation = '".$_SERVER["PHP_SELF"]."?".($qs[0]!=''?$qs[0]."&":'')."chvsde='+year+'-'+month+'-'+day+'&vsde_obj='+objCount;\n";
			//$js .= "\n";
			//$js .= "	document.location = strLocation;\n";
		}

		$js .= "\n";
		
		$js .= "}\n";

		return $js;
	}
	//=====================================================================================================================

};

?>
